package com.example.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/** 分布式事务回滚
 * 1用户下单发送消息到stock-event-exchange交换机，同时指定路由键为stock.release，
 * 2 通过stockLockedBinding方法绑定到stock.delay.queue延时队列
 * 3 到达指定过期时间后stockDelayQueue演示队列将消息重新放回stock-event-exchange交换机，
 * 并重新指定路由键为stock.release.stock
 * 4 通过stockReleaseBinding方法路由键stock.release.#模糊绑定到stock.release.stock.queue队列
 *
 */
@Configuration
public class MyRabbitConfig {
//    @RabbitListener(queues = "stock.release.stock.queue")
//    public void  handle(Message message){
//
//    }
    @Bean
    public MessageConverter messageConverter(){
        return  new Jackson2JsonMessageConverter();
    }
    @Bean
    public Exchange stockEventExchange(){
        /**
         * tring name交换机名称
         * boolean durable, 是否持久化
         * boolean autoDelete,是否自动删除
         * Map<String, Object> arguments 额外参数
         */
      return   new TopicExchange("stock-event-exchange",true,false);
    }

    /**
     * 普通队列
     * @return
     */
    @Bean
    public Queue stockReleaseStockQueue(){
        /**
         * String name, boolean
         * durable, boolean exclusive,
         * boolean autoDelete,
         * Map<String, Object> arguments
         */
        return  new Queue("stock.release.stock.queue",true,false,false,null);
    }

    /**
     * 延迟队列
     * @return
     */
    @Bean
    public  Queue stockDelayQueue(){
        /**
         * x-dead-letter-exchange: order-event-exchange 信死了要给哪个交换机
         * x-dead-letter-routing-key:order.release.order 信死了要绑定哪个路由键
         * x-message-ttl: 60000 过期时间 什么时候信死
         * arguments:到期时间到了之后成为死信，将信息重新发回order-event-exchange交换机，
         * 同时改变路由键为order.release.order，就可以绑定到orderReleaseOrderQueue队列，让消费者监听，从而解锁库存
         *
         */
        Map<String,Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        arguments.put("x-dead-letter-routing-key","stock.release.stock");
        arguments.put("x-message-ttl",120000);
        return  new Queue("stock.delay.queue",true,false,false,arguments);
    }

    /**
     * 绑定普通队列
     * @return
     */
    @Bean
    public Binding stockReleaseBinding(){
       return new Binding("stock.release.stock.queue",
               Binding.DestinationType.QUEUE,
               "stock-event-exchange",
               "stock.release.#",
               null);
    }

    /**
     * 绑定延迟队列
     * @return
     */
    @Bean
    public Binding stockLockedBinding(){
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }

}
